﻿//======================================================================
//
//        Copyright : Zhengzhou Strawberry Computer Technology Co.,LTD.
//        All rights reserved
//        
//        Application:NRapid MVC framework
//        Filename :CookieManager.cs
//        Description :Cookie管理类
//
//        created by Lucas at  2015-6-30`
//     
//        WebSite:http://www.NRapid.com
//
//======================================================================
using System;
using System.Collections.Generic;

namespace ${project}.${app}.Code.Data
{
    public class CookieManager
    {
		private NRapid.Owin.HtmlWriter writer;
		private IDictionary<string, string> cookies;
        private CookieInfo _cookieInfo;

        private NRapid.ServerType _serverType = NRapid.ServerType.UnKnown;
        public CookieManager()
        {
            this._serverType = NRapid.ServerType.IsStatic;
            _cookieInfo = new CookieInfo();
        }
		public CookieManager(NRapid.Owin.HtmlWriter writer,IDictionary<string,string> cookies)
        {
            this._serverType = NRapid.ServerType.NRapidOwin;
            this.cookies = cookies;
            this.writer = writer;
            _cookieInfo = new CookieInfo();
        }
		public string this[string key]
        {
            get
            {
				string val=null;
				if (this._serverType == NRapid.ServerType.NRapidOwin)
                {
                    if (cookies.ContainsKey("${cookiePrefix}"+key))
                    {
                        return cookies["${cookiePrefix}"+key];
                    }
                    else
                    {
                        return null;
                    }
                }

				return val;
            }
            set
            {
                if (this._serverType == NRapid.ServerType.NRapidOwin)
                {
                    if (value == null)
                    {
                        writer.SetExpiredCookie("${cookiePrefix}"+key);
                    }
                    else
                    {
                        writer.SetCookie("${cookiePrefix}"+key, value);
                    }
                }
            }
        }
		public string session_id
        {
            get{_cookieInfo.session_id = this["session_id"];return _cookieInfo.session_id;}
			set{_cookieInfo.session_id = value; this["session_id"]=_cookieInfo.session_id;}
        }
        public string vcheck
        {
            get{_cookieInfo.vcheck = this["vcheck"];return _cookieInfo.vcheck;}
			set{_cookieInfo.vcheck = value; this["vcheck"]=_cookieInfo.vcheck;}
        }
        public string check
        {
            get{_cookieInfo.check = this["check"];return _cookieInfo.check;}
			set{_cookieInfo.check = value; this["check"]=_cookieInfo.check;}
        }
        $foreach(declaraton in declarations)
        public ${declaraton.typeName} ${declaraton.varName}
        {
            get
            {
				${if(declaraton.isString==true)}
					_cookieInfo.${declaraton.varName} = this["${declaraton.varName}"];
				${else}
					${declaraton.typeName}.TryParse(this["${declaraton.varName}"],out _cookieInfo.${declaraton.varName});
				${end}
				return _cookieInfo.${declaraton.varName};
            }
            set
            {
                _cookieInfo.${declaraton.varName} = value;
				${if(declaraton.isString==true)}
					this["${cookiePrefix}${declaraton.varName}"]= value;
				${else}
					this["${cookiePrefix}${declaraton.varName}"]= value.ToString();
				${end}
            }
        }
        $end
    }
}